package com.tn.controller.login;

import com.tn.controller.alarm.common.model.CommonVO;
import com.tn.controller.alarm.common.model.login.vo.LogInVO;
import com.tn.exception.DatabaseOperationFailedException;
import com.tn.service.login.ILonInService;
import com.tn.utils.CheckParamUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 登录controller
 *
 * @program: tn_aucy_java
 * @author: yangjiayao
 * @create: 2021/12/22
 */
@Slf4j
@RestController
@RequestMapping("/aucy")
public class LogInController {
    /**
     * service
     */
    private ILonInService lonInService;

    /**
     * 注入
     *
     * @param lonInService service
     */
    @Autowired
    public void setLonInService(ILonInService lonInService) {
        this.lonInService = lonInService;
    }

    /**
     * 用户登录
     *
     * @param logInVO 入参
     * @return 是否允许登录
     */
    @RequestMapping("/login")
    public CommonVO<Object> login(@RequestBody LogInVO logInVO) throws DatabaseOperationFailedException {
        CheckParamUtil.checkBlankRequiredParam(logInVO.getPassWord(), logInVO.getUserName());
        return new CommonVO<>(lonInService.logIn(logInVO.getUserName(), logInVO.getPassWord()));
    }

    /**
     * 退出登录
     *
     * @param logInVO 入参
     * @return 退出登录结果
     *
     */
    @RequestMapping("/logout")
    public CommonVO<Object> logout(@RequestBody LogInVO logInVO) throws DatabaseOperationFailedException {
        CheckParamUtil.checkBlankRequiredParam(logInVO.getUserName());
        lonInService.logout(logInVO.getUserName());
        return new CommonVO<>();
    }
}
